// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#ifndef HOST_CONTROL_H_
#define HOST_CONTROL_H_

#include <stdint.h>
#include <stdbool.h>
#include "status/rot_status.h"


/**
 * A platform-independent API for controlling access to the protected flash and the processor that
 * uses that flash.
 */
struct host_control {
	/**
	 * Enable and disable the signal to the host processor that will hold it in reset.
	 *
	 * @param control The control instance for the host processor whose state should be changed.
	 * @param reset true to put the host processor in reset, false to release reset.
	 *
	 * @return 0 if the reset control was changed successfully or an error code.
	 */
	int (*hold_processor_in_reset) (struct host_control *control, bool reset);

	/**
	 * Indicate if the host processor is being held in reset.
	 *
	 * @param control The control instance to query.
	 *
	 * @return 1 if the host processor is being held in reset, 0 if not, or an error code.
	 */
	int (*is_processor_held_in_reset) (struct host_control *control);

	/**
	 * Indicate if the host processor is current in reset.  This can be due to being held in reset
	 * by the RoT or a normal reset operation by the host processor.
	 *
	 * @param control The control instance to query.
	 *
	 * @return 1 if the host processor is current in reset, 0 if not, or an error code.
	 */
	int (*is_processor_in_reset) (struct host_control *control);

	/**
	 * Enable and disable access to the protected flash devices from the host processor.
	 *
	 * @param control The control instance for the host processor flash to change access state for.
	 * @param enable true to enable processor access to the flash, false to enable RoT access.
	 *
	 * @return 0 if the flash access was changed successfully or an error code.
	 */
	int (*enable_processor_flash_access) (struct host_control *control, bool enable);

	/**
	 * Indicate if the host processor currently has access to the protected flash.
	 *
	 * @param control The control instance to query.
	 *
	 * @return 1 if the host processor has flash access, 0 if the RoT has flash access, or an error
	 * code.
	 */
	int (*processor_has_flash_access) (struct host_control *control);
};


#define	HOST_CONTROL_ERROR(code)		ROT_ERROR (ROT_MODULE_HOST_CONTROL, code)

/**
 * Error codes that can be generated by host control driver.
 */
enum {
	HOST_CONTROL_INVALID_ARGUMENT = HOST_CONTROL_ERROR (0x00),		/**< Input parameter is null or not valid. */
	HOST_CONTROL_NO_MEMORY = HOST_CONTROL_ERROR (0x01),				/**< Memory allocation failed. */
	HOST_CONTROL_HOLD_RESET_FAILED = HOST_CONTROL_ERROR (0x02),		/**< The host reset has not been enabled/disabled. */
	HOST_CONTROL_HOLD_CHECK_FAILED = HOST_CONTROL_ERROR (0x03),		/**< Could not determine if the host is being held in reset. */
	HOST_CONTROL_RESET_CHECK_FAILED = HOST_CONTROL_ERROR (0x04),	/**< Could not determine if the host is in reset. */
	HOST_CONTROL_FLASH_ACCESS_FAILED = HOST_CONTROL_ERROR (0x05),	/**< Host flash access has not been enabled/disabled. */
	HOST_CONTROL_FLASH_CHECK_FAILED = HOST_CONTROL_ERROR (0x06),	/**< Could not determine if the hosh has flash access. */
};


#endif /* HOST_CONTROL_H_ */
